﻿using CoffeeShop.BIZ;
using CoffeeShop.Common;
using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CoffeeShop.Controllers
{
    public class AreaController : Controller
    {
        private AreaBiz _areaBiz = new AreaBiz();
        private OfficeBiz _officeBiz = new OfficeBiz();

        /// <summary>
        /// List area page
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Admin, Staff")]
        public ActionResult Index()
        {
            List<Area> listArea = new List<Area>();
            listArea = _areaBiz.GetListAreas();
            return View(listArea);
        }

        /// <summary>
        /// Add new an area
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public string CreateArea()
        {
            string areaName = Request.Params["nameOfArea"].Trim();
            string officeId = Request.Params["idOfOffice"].Trim();
            Area areaObject = new Area();
            areaObject = _areaBiz.ConvertToObject(areaName, Convert.ToInt32(officeId));
            if (_areaBiz.IsExistArea(areaName, Convert.ToInt32(officeId)))
            {
                return "ExistedArea";
            }
            else
            {
                if (_areaBiz.CreateArea(areaObject))
                {
                    return "AddSuccessful";
                }
                else
                {
                    return "FailAddNewArea";
                }
            }
        }

        /// <summary>
        /// Update an Area
        /// </summary>
        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public string UpdateArea()
        {
            string areaId = Request.Params["areaId"].Trim();
            string officeId = Request.Params["officeId"].Trim();
            string areaName = Request.Params["areaName"].Trim();

            Area areaObject = new Area();
            areaObject = _areaBiz.ConvertToObject(areaName, Convert.ToInt32(officeId), Convert.ToInt32(areaId));
            if (_areaBiz.IsExistedWhenUpdate(areaObject.OfficeId, areaObject.AreaName, Convert.ToInt32(areaId)))
            {
                return "ExistedArea";
            }
            else
            {
                if (_areaBiz.UpdateArea(areaObject))
                {
                    return "UpdateSuccessful";
                }
                else
                {
                    return "FailUpdateArea";
                }
            }
        }

        /// <summary>
        /// Delete an area in Database
        /// </summary>
        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public string DeleteArea(string areaId)
        {
            var isDeleteSuccess = _areaBiz.DeleteArea(Convert.ToInt32(areaId));
            if (isDeleteSuccess)
            {
                return "DeleteSuccessful";
            }
            else
            {
                return "FailDeleteOffice";
            }
        }

        /// <summary>
        /// Get list office to send value to object
        /// </summary>
        /// <returns></returns>
        public JsonResult GetListOffice()
        {
            List<Office> listOffice = new List<Office>();
            listOffice = _officeBiz.GetListActiveOffice();

            List<Object> listResult = new List<Object>();
            foreach (var office in listOffice)
            {
                var result = CmnFunc.ConvertDynamicObjectWithFullAttr(office);
                listResult.Add(result);
            }
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Staff")]
        public PartialViewResult LoadAllAreas()
        {
            List<Area> listAllAreas = new List<Area>();
            listAllAreas = _areaBiz.GetListAreas();
            return PartialView("_AreasTable", listAllAreas);
        }

        public JsonResult GetAreaById(string areaId)
        {
            Area area = new Area();
            area = _areaBiz.GetAreaById(Convert.ToInt32(areaId));
            return Json(new { AreaId = area.AreaId, AreaName = area.AreaName, OfficeId = area.OfficeId, IsDeleted = area.IsDelete }
                , JsonRequestBehavior.AllowGet);
        }
    }
}
